// pages/management/administration/index.js
const netWorked = require('../../../utils/nework.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: true,
    num: 0,
    goodsImg: [],
    images: [],
    list: [],
    id: '',
    index: '', //商品号item_no
    indexTo: 0,
    datass: [],
    money: 0,
    imges: 'https://odimgs.oss-cn-shenzhen.aliyuncs.com/9c662e9e654e4655bc4849d95cb4ee48.png',
    counts: 0, //总数量
    picres: 0, //总价格
    savesTides: [] //存储所有
  },
  // 选好了
  finallyBut2: function(e) {
    let goodsAll = this.data.savesTides
    let that = this
    if (goodsAll.length == 0) {
      let savesTides = wx.getStorageSync('savesTides')
      that.setData({
        savesTides
      })
    }

    wx.setStorageSync('savesTides', this.data.savesTides)
    wx.navigateTo({
      url: '/pages/management/particulars/index',
    })
    // console.log(wx.getStorageSync('savesTides'))
  },
  // 已选择
  finallyBut1: function(e) {
    wx.navigateTo({
      url: '/pages/management/particulars/index',
    })
  },
  // 图片详情
  particulars: function(e) {
    let img = e.currentTarget.dataset.item
    this.setData({
      imges: img
    })
  },
  //显示隐藏遮罩层
  sected: function(e) {
    let that = this
    let index = e.currentTarget.dataset.id
    console.log(e)
    this.setData({
      showView: (!that.data.showView),
      index,
    })
    // console.log(e.currentTarget.dataset.id)
    this.test()
  },
  // 确定
  confirm: function(e) {
    let that = this
    let goodsImgs = [] //记录选择的商品
    let pirtce = this.data.pirtce;
    let counts = this.data.counts;
    let savesTides = this.data.savesTides
    let name = this.data.images.item_name
    let imagesGood = this.data.datass
    let item_no = this.data.index
    let unit = parseInt(this.data.images.price)
    for (let i = 0; i < imagesGood.length; i++) {
      if (imagesGood[i].sum > 0) {
        // console.log(imagesGood[i].img + "sss")
        goodsImgs.push({
          item_img_path: imagesGood[i].img,
          real_qty: imagesGood[i].sum,
          pirtce: unit,
          money: imagesGood[i].money,
          item_no: item_no
        })
      }
    }
    if (goodsImgs.length > 0) {
      savesTides.push({
        name: name,
        goodsImgs: goodsImgs,
        counts: counts,
        unit: unit,
        
      })
      that.setData({
        showView: (!that.data.showView),
        savesTides
      })
    } else {
      that.setData({
        showView: (!that.data.showView),
      })
    }


  },
  // 减少
  reduce: function(e) {
    let indexsd = e.currentTarget.dataset.index
    let that = this
    let imagesGood = this.data.datass
    let num = parseInt(imagesGood[indexsd].sum);
    if (num <= 0) {
      return false;
    }
    num = num - 1
    imagesGood[indexsd].sum = num
    this.setData({
      datass: imagesGood
    })
    this.collect()
  },
  // 增加
  add: function(e) {
    // console.log(e)
    let indexofGood = e.currentTarget.dataset.index
    let that = this
    let imagesGood = this.data.datass;
    let money = imagesGood[indexofGood].money; //获取单行的金额
    let unit = parseInt(this.data.images.price)
    let num = parseInt(imagesGood[indexofGood].sum) //获取单行的数量
    num = num + 1
    money = num * unit //计算单行金额
    imagesGood[indexofGood].sum = num
    imagesGood[indexofGood].money = money
    this.setData({
      datass: imagesGood
    })
    // console.log(imagesGood)
    this.collect()
  },
  // 获取总价格和总数量
  collect: function(e) {
    let imagesGood = this.data.datass
    let that = this
    let pirtce = 0;
    let counts = 0;
    // let money = 0;
    let unit = parseInt(this.data.images.price)
    for (let i = 0; i < imagesGood.length; i++) {
      pirtce += imagesGood[i].sum * unit
      counts += parseInt(imagesGood[i].sum)
      that.setData({
        pirtce,
        counts,
      })
    }
    // console.log(this.data.money)
  },
  // 获取数据
  goodsImg: function(e) {
    let id = wx.getStorageSync("id")
    console.log(id)
    let that = this
    let where = "main_supcust='" + id + "'"
    netWorked.netWork({
      url: 'Purchase/XuanShangping?oper_id=1001&where=' + where,
      method: "POST",
      success: function(res) {
        console.log(res)
        let data = res.data;
        for (let i in data) {
          data[i].item_img_paths = data[i].item_img_paths.split(",")
        }
        // console.log(data)
        that.setData({
          goodsImg: data,
        })
      }
    })

  },
  // 获取遮罩层数据
  test: function(e) {
    let that = this
    let index = this.data.index
    let where = "item_no='" + index + "'"
    let das = this.data.datass
    if (das.length == 0) {
      netWorked.netWork({
        url: 'Purchase/XuanShangping?oper_id=1001&where=' + where,
        method: "POST",
        success: function(res) {
          let data = res.data;
          for (let i in data) {
            data[i].item_img_paths = data[i].item_img_paths.split(",")
          }
          that.setData({
            images: data[0]
          })
          for (let i in data[0].item_img_paths) {
            das.push({
              id: data[0].item_no,
              img: data[0].item_img_paths[i],
              sum: '0',
              money: 0
            });
          }
          that.setData({
            datass: das
          })
          // console.log(that.data.images)
        }
      })
    } else {
      das = []
      netWorked.netWork({
        url: 'Purchase/XuanShangping?oper_id=1001&where=' + where,
        method: "POST",
        success: function(res) {
          let data = res.data;
          for (let i in data) {
            data[i].item_img_paths = data[i].item_img_paths.split(",")
          }
          that.setData({
            images: data[0]
          })
          for (let i in data[0].item_img_paths) {
            das.push({
              id: data[0].item_no,
              img: data[0].item_img_paths[i],
              sum: '0',
              money: 0
            });
          }
          that.setData({
            datass: das
          })
          // console.log(that.data.images)
        }
      })
    }


  },
  // 保存

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.goodsImg()
    // this.listTo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})